package com.rds.benchmark.operator;

import com.rds.benchmark.util.MakeValue;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.commands.JedisCommands;

import java.io.IOException;

public class PublishOperator extends TypeOperatorBase {

    private Object conn;

    private String key;

    private final String value = MakeValue.value;

    @Override
    public void init(Object conn, String key, int idx,String mode) {
        this.conn = conn;
        this.key = key;
    }

    @Override
    public Long clear() {
        return ((JedisCommands) conn).del(key);
    }

    @Override
    public void operate() throws IOException {
        if (conn instanceof Jedis) {
            Jedis jedis = (Jedis) conn;
            jedis.publish(key, value);
        } else if (conn instanceof JedisCluster) {
            JedisCluster cluster = (JedisCluster) conn;
            cluster.publish(key, value);
        }
        times++;
    }

}
